<template>
  <div id="page-health">
    <!-- 查询 -->
    <div class="page-search-list">
      <el-form
        label-position="left"
        :model="search_form"
        :inline="true"
        ref="healthFromRef"
      >
        <el-form-item
          v-for="(item, i) in search_info"
          :label="item.label"
          :key="i"
          style="margin-bottom: 5px;"
        >
          <template v-if="item.prop === 'start_time'">
            <template v-if="!is_time_period">
              <el-date-picker
                type="dates"
                v-model="search_form.time"
                placeholder="选择一个或多个日期"
                key="time"
                value-format="yyyy-MM-dd"
                :append-to-body="false"
                size="small"
              ></el-date-picker>
            </template>
            <template v-else>
              <el-date-picker
                v-model="search_form.time_period"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                key="time_period"
                value-format="yyyy-MM-dd"
                :append-to-body="false"
                size="small"
              ></el-date-picker>
            </template>
          </template>
          <template v-else-if="item.prop === 'cid'">
            <select-community
              :cid.sync="search_form[item.prop]"
              :pid.sync="search_form.pid"
              :bid.sync="search_form.bid"
              :sid.sync="search_form.sid"
            ></select-community>
          </template>
          <template v-else-if="item.prop === 'health_status'">
            <el-select
              v-model="search_form[item.prop]"
              clearable
              placeholder="请选择健康状态"
              size="small"
            >
              <el-option
                v-for="item in health_arr"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
          <template v-else-if="item.prop === 'is_acid'">
            <el-select
              v-model="search_form[item.prop]"
              clearable
              placeholder="请选择核酸状态"
              size="small"
            >
              <el-option
                v-for="item in acid_status"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
          <template v-else>
            <el-input
              v-model="search_form[item.prop]"
              size="small"
              :placeholder="`请输入${item.label}`"
            ></el-input>
          </template>
        </el-form-item>
        <el-form-item>
          <el-button class="el-icon-search" size="small" @click.native="search">
            搜索
          </el-button>
          <el-button
            type="primary"
            class="el-icon-refresh"
            size="small"
            @click.native="resetSearchFields"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="page-botton-list">
      <el-button
        plain
        class="el-icon-date"
        size="mini"
        @click.native="is_time_period = !is_time_period"
      >
        {{ is_time_period ? '时间查询' : '时间段查询' }}
      </el-button>
      <el-button
        type="primary"
        plain
        class="el-icon-bell"
        size="mini"
        @click="health_dialog_visible = true"
        key="add_button"
      >
        新增上报
      </el-button>
      <el-button
        type="danger"
        plain
        class="el-icon-delete"
        size="mini"
        :disabled="is_delete"
        @click="deleteHealth"
        key="delete_arr_button"
      >
        删除
      </el-button>
      <ExportExcel
        style="margin-left: 0.625rem;"
        :id="'health_tabelId'"
        :label="'导出'"
        :multipleSelection="multiple_selection"
        :tableData="health_tabel"
        :tableInfo="tableInfo"
      ></ExportExcel>
    </div>
    <!-- 表格 -->
    <div class="page-table">
      <el-table
        ref="healthTableRef"
        :data="health_tabel"
        style="width: 100%;"
        id="health_tabelId"
        @selection-change="selectTabel"
      >
        <el-table-column type="selection" />
        <el-table-column
          v-for="(column, i) in tableInfo"
          :key="i"
          :label="column.label"
          align="center"
        >
          <template slot-scope="scope">
            <template v-if="column.prop === 'health_status'">
              <el-tag type="success" v-if="scope.row[column.prop] == 0">
                阴性
              </el-tag>
              <el-tag type="danger" v-else>阳性</el-tag>
            </template>
            <template v-else-if="column.prop === 'acid_img'">
              <img
                :src="scope.row[column.prop]"
                alt="核算结果图片"
                style="width: 7.5rem; height: 7.5rem;"
              />
            </template>
            <template v-else>{{ scope.row[column.prop] }}</template>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px" align="center">
          <template slot-scope="scope">
            <el-button
              type="warning"
              plain
              size="mini"
              class="el-icon-delete"
              @click="deleteHealth(scope.row.uid, scope.row.uname)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageCount"
        :page-sizes="[10, 100, 200, 300, 400]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tatal"
        class="upload-list-pagination"
      ></el-pagination>
    </div>

    <!-- 添加健康上报弹窗 -->
    <el-dialog
      title="添加健康上报"
      :visible.sync="health_dialog_visible"
      :append-to-body="true"
      width="40%"
    >
      <el-form
        :model="add_health_dialog"
        label-position="left"
        :rules="rules_dialog"
      >
        <el-form-item
          v-for="(item, i) in dialog_info"
          label-width="80px"
          :key="i"
          :label="item.label"
          :prop="item.prop"
        >
          <template v-if="item.prop === 'cid'">
            <select-community
              :cid.sync="add_health_dialog[item.prop]"
              :pid.sync="add_health_dialog.pid"
              :bid.sync="add_health_dialog.bid"
              :sid.sync="add_health_dialog.sid"
            ></select-community>
          </template>
          <template v-else>
            <el-input
              size="small"
              v-model="add_health_dialog[item.prop]"
            ></el-input>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="addClick">
          确定添加
        </el-button>
      </div>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog
      title="提示"
      :visible.sync="delete_dialog_visible"
      width="30%"
      center
      :append-to-body="true"
    >
      <div>
        是否删除
        <span v-for="(item, i) in delete_user_arr" :key="i">
          {{ item.name }}
        </span>
        的健康上报
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="falsedeleteHealth">取 消</el-button>
        <el-button type="primary" @click="truedeleteHealth">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 工具类
import { isNull } from '@/utils/tool.js'
export default {
  computed: {
    is_delete() {
      return !this.multiple_selection.length > 0
    },
  },
  watch: {
    search_form: {
      handler(val) {
        console.log(val)
      },
    },
    health_dialog_visible: {
      handler(val) {
        if (!val) {
          this.add_health_dialog = {}
          this.inner_visible = false
        }
      },
    },
    inner_visible: {
      handler(val) {
        if (!val) {
          this.temppass_address = {
            id: '',
            time: '',
            address: '',
          }
          if (isNull(this.$refs.pass_addressRef))
            this.$refs.pass_addressRef.resetAddress()
        }
      },
      immediate: true,
    },
    delete_dialog_visible: {
      handler(val) {
        if (!val) {
          this.delete_user_arr = []
          this.$refs.healthTableRef.clearSelection()
        }
      },
    },
  },
  data() {
    return {
      tatal: 10, //总数据
      pageCount: 1, // 页数
      pageSize: 10, // 页的大小
      health_arr: [
        { value: 1, label: '阳性' },
        { value: 0, label: '阴性' },
      ],
      acid_status: [
        { value: 1, label: '已核酸' },
        { value: 0, label: '未核酸' },
      ],
      // 查询信息
      search_form: {
        cid: '', // 根据小区编号查询所有人行程
        pid: '',
        bid: '',
        sid: '',
        uname: '', // 根据用户名查询所有人行程
        time: [], // 选择时间 可多选
        time_period: [], // 时间段
        admin_id: '', // 根据管理员编号查询该管理员发布的行程上报
        health_status: '',
        is_acid: '', //是否核酸，1核酸 0未核算
      },
      search_info: {
        cid: { label: '社区', prop: 'cid' },
        uname: { label: '居民姓名', prop: 'uname' },
        admin_id: { label: '管理员编号', prop: 'admin_id' },
        is_acid: { label: '是否核酸', prop: 'is_acid' },
        health_status: { label: '健康状态', prop: 'health_status' },
        start_time: { label: '核酸日期', prop: 'start_time' },
      },
      is_time_period: false, // 是否以时间段查询
      tableInfo: {
        cid: { label: '社区', prop: 'cid' },
        uname: { label: '居民姓名', prop: 'uname' },
        address: { label: '住址', prop: 'address' },
        acid_time: { label: '核酸日期', prop: 'acid_time' },
        health_status: { label: '健康状态', prop: 'health_status' },
        acid_address: { label: '核酸采集点', prop: 'acid_address' },
        acid_img: { label: '核酸结果图片', prop: 'acid_img' },
      },
      health_tabel: [
        {
          cid: '宾海小区',
          uid: '10005',
          uname: '张三',
          address: '福建省漳州市芗城区滨海小区4栋5楼',
          acid_time: '2020-5-6',
          health_status: 0,
          acid_address: '滨海小区北门核酸采集点',
          acid_img:
            'https://p0.itc.cn/q_70/images03/20210518/8ab0b83b7d6c4bb39d975aecd37f6c4d.jpeg',
        },
      ],
      multiple_selection: [], // 表格选择
      health_dialog_visible: false, // 弹窗是否可见
      add_health_dialog: {
        title: '',
        content: '',
        cid: '',
        pid: '',
        bid: '',
        sid: '',
        dataTime: '',
      },
      dialog_info: {
        title: { label: '标题', prop: 'title' },
        content: { label: '内容', prop: 'content' },
        cid: { label: '通知社区', prop: 'cid' },
      },
      rules_dialog: {
        title: [{ required: true, message: '请输入标题！', trigger: 'blur' }],
        cid: [{ required: true, message: '请选择社区！', trigger: 'blur' }],
      },
      // 单个途径地对象
      temppass_address: {
        id: '',
        time: '',
        address: '',
      },
      delete_dialog_visible: false, //删除弹窗是否可见
      delete_user_arr: [], // 删除的用户数组
    }
  },
  created() {},
  methods: {
    /* 请求 */
    /* DOM 操作 */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
    // 搜索
    search() {
      console.log(this.search_form)
      const data = this.travelFrm
    },
    // 清空搜索条件
    resetSearchFields() {
      this.search_form = {
        cid: '',
        uname: '',
        time: [],
        time_period: [],
        admin_id: '',
      }
    },
    // 表格选择
    selectTabel(val) {
      this.multiple_selection = val
      console.log(this.multiple_selection)
    },
    // 点击 确定 最外层弹窗
    addClick() {
      this.health_dialog_visible = false
      console.log(this.add_health_dialog)
      this.$message({
        message: '添加成功',
        type: 'success',
      })
    },
    // 点击 取消 最外弹窗
    cancelDialog() {
      this.health_dialog_visible = false
    },
    // 删除
    deleteHealth(id, name) {
      this.delete_dialog_visible = true
      if (isNull(id) && typeof id === 'string') {
        // 单个删除
        let user = {
          id,
          name,
        }
        this.delete_user_arr.push(user)
        console.log('删除单个')
      } else {
        this.multiple_selection.forEach((item) => {
          let user = {
            id: item.uid,
            name: item.uname,
          }
          this.delete_user_arr.push(user)
        })
        console.log('删除数组')
      }
      console.log(this.delete_user_arr)
    },
    falsedeleteHealth() {
      this.delete_dialog_visible = false
      this.$message('已取消删除')
    },
    truedeleteHealth() {
      this.delete_dialog_visible = false
      this.$message({
        message: '删除成功',
        type: 'success',
      })
    },
  },
}
</script>

<style lang="less" scoped>
#page-health {
  padding: 0 0.5rem;
  .page-table {
    .el-button {
      margin: 2px 0;
    }
  }
  .el-collapse-title-div {
    display: flex;
    justify-content: space-around;
    width: 90%;
  }
  .el-form-ul {
    min-height: 2.375rem;
    border: 1px solid #dcdfe6;
  }
  /deep/.el-collapse {
    border: none;
    background-color: rgba(255, 255, 255, 0);
  }
  /deep/.el-collapse-item__content {
    padding-bottom: 0;
  }
}
.page-botton-list {
  margin-bottom: 0.625rem;
}
.ul-address-date {
  display: flex;
  .ul-address-date-item {
    flex: 1;
    margin-right: 1.25rem;
    &:last-child {
      margin-right: 0;
    }
  }
}
/deep/.el-select {
  width: 100%;
}
.address-tag {
  margin-right: 0.625rem;
  &:last-child {
    margin-right: 0;
  }
}
.upload-list-pagination {
  margin-top: 1.8rem;
  display: flex;
  justify-content: center;
}
</style>
